﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace WebRTC.Controllers
{
    public class WebRtcController : Controller
    {
        // GET: WebRtc
        public ActionResult Index()
        {
            ViewData["USERID"] = System.Guid.NewGuid().ToString("N");
            ViewData["IP"] = getIP();
            ViewData["Host"] = Request.Headers["Host"];           
            return View();
        }




        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <returns></returns>
        public static string getIP()
        {

            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(System.Web.HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address == String.Empty)
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        IP4Address = IPA.ToString();
                        break;
                    }
                }
            }



            return IP4Address;
        }
    }
}